﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DDNY.WebPortals.Common
{
    public class Transliterator
    {
        public static string FromCyrToLatin(string input)
        {
            string result;
            input = input.Replace(" ", "-");
            StringBuilder resultBuilder = new StringBuilder();
            char[] inputChars = input.ToCharArray();

            foreach (char c in inputChars)
            {
                if (char.IsLetter(c))
                {
                    string transliteratedSymbol = TransliterateSingCharFromCyrToLating(c);
                    resultBuilder.Append(transliteratedSymbol);
                }
                else
                {
                    resultBuilder.Append(c);
                }
            }
            result = resultBuilder.ToString();
            return result;
        }

        private static string TransliterateSingCharFromCyrToLating(char c)
        {
            string result;
            if (CyrToLatinCharsDic == null)
            {
                CyrToLatinCharsDic = LoadCyrToLatingDic();
            }
            if (CyrToLatinCharsDic.ContainsKey(c))
            {
                result = CyrToLatinCharsDic[c];
            }
            else
            {
                result = c.ToString();
            }

            return result;
        }

        private static Dictionary<char, string> LoadCyrToLatingDic()
        {
            Dictionary<char, string> result = new Dictionary<char, string>();
            result.Add('а', "a");
            result.Add('б', "b");
            result.Add('в', "v");
            result.Add('г', "g");
            result.Add('д', "d");
            result.Add('е', "e");
            result.Add('ж', "j");
            result.Add('з', "z");
            result.Add('и', "i");
            result.Add('й', "y");
            result.Add('к', "k");
            result.Add('л', "l");
            result.Add('м', "m");
            result.Add('н', "n");
            result.Add('о', "o");
            result.Add('п', "p");
            result.Add('р', "r");
            result.Add('с', "s");
            result.Add('т', "t");
            result.Add('у', "u");
            result.Add('ф', "f");
            result.Add('х', "h");
            result.Add('ц', "c");
            result.Add('ч', "ch");
            result.Add('ш', "sh");
            result.Add('щ', "sht");
            result.Add('ъ', "a");
            result.Add('ь', "");
            result.Add('ю', "yu");
            result.Add('я', "ya");

            return result;
        }

        private static Dictionary<char, string> CyrToLatinCharsDic;


    }
}
